﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using OnLineExam.Data;
using HHG.Common;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// MultiSelectHandler 的摘要说明
    /// </summary>
    public class MultiProblemHandler : IHttpHandler
    {
        private OnlineExamEntities _dbContext;
        private EfRepository<Data.MultiProblem> _multiRepository;

        public MultiProblemHandler() {

            _dbContext = new OnlineExamEntities();
            _dbContext.Configuration.LazyLoadingEnabled = false;
            _multiRepository = new EfRepository<Data.MultiProblem>(_dbContext);
        }

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            string type = context.Request.Params["type"];
            var paperId = context.Request.Form["id"].GetInt();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);

                    var query = _multiRepository.TableNoTracking;
                    if (paperId > 0)
                        query = query.Where(s => s.PaperID == paperId);



                    int RecordCount = query.Count();
                    var items = query.OrderBy(s => s.ID).Skip((page - 1) * rows).Take(rows).Select(s => new { ID = s.ID, Title = s.Title, });


                    context.Response.Write(new DataGrid(RecordCount, items).SerializeObject());
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息
                    var ID = context.Request.Params["ID"].GetInt();
                    if (ID > 0)
                    {
                        var entity = _multiRepository.GetById(ID);
                        context.Response.Write(entity.SerializeObject());
                       
                    } 
                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                   
                    //ID = context.Request.Params["ID"];
                    //var o_model = bll.GetModel(int.Parse(ID));
                    var new_model = new OnLineExam.Data.MultiProblem();
                    //new_model.ID = int.Parse(ID);
                    
                    new_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
                    new_model.Title = context.Request.Params["txtTitle"].Trim();
                    new_model.AnswerA = context.Request.Params["txtA"].Trim();
                    new_model.AnswerB = context.Request.Params["txtB"].Trim();
                    new_model.AnswerC = context.Request.Params["txtC"].Trim();
                    new_model.AnswerD = context.Request.Params["txtD"].Trim();
                    new_model.AnswerE = context.Request.Params["txtE"].Trim();
                    new_model.AnswerF = context.Request.Params["txtF"].Trim();
                    new_model.AnswerG = context.Request.Params["txtG"].Trim();
                    new_model.Answer = context.Request.Params["txtAnswer"].Trim();

                    _multiRepository.Insert(new_model);
            
                    context.Response.Write(0);//修改成功，返回ID,失败，返回0
                     
                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    ID = context.Request.Params["hdID"].GetInt();
                     
                    new_model = _multiRepository.GetById(ID); 
					new_model.ID = ID;
                 
                    new_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
                    new_model.Title = context.Request.Params["txtTitle"];
                    new_model.AnswerA = context.Request.Params["txtA"];
                    new_model.AnswerB = context.Request.Params["txtB"];
                    new_model.AnswerC = context.Request.Params["txtC"];
                    new_model.AnswerD = context.Request.Params["txtD"];
                    new_model.AnswerE = context.Request.Params["txtE"];
                    new_model.AnswerF = context.Request.Params["txtF"];
                    new_model.AnswerG = context.Request.Params["txtG"];
                    new_model.Answer =  context.Request.Params["txtAnswer"].ToUpper();

                    _multiRepository.Update(new_model);
                    context.Response.Write(ID);//修改成功，返回ID 
                    
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"].GetInt();

                    _multiRepository.Delete(_multiRepository.GetById(ID));
                    context.Response.Write(ID);//删除成功，返回ID
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}